﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Subgurim.Controles;
using System.Collections;
using Kohvik.Kohvikuteenus;

namespace Kohvik {

    public partial class _Default : System.Web.UI.Page {

        GLatLng latlngTln = new GLatLng(59.4388619, 24.7544715);
        GLatLng latlngEst = new GLatLng(58.595272, 25.013607);
        GLatLng latlngNull = new GLatLng(0,0);
        GLatLng LatLngMina;
        List<EntityKohvik> kohvikud;
        bool loaded = false;
        String cafeInfo="";
        List<MarkerCafe> markerCafeList = new List<MarkerCafe>();
        Subgurim.Controles.GMarker marker;
        Subgurim.Controles.GMarker markerMina;

        protected void Page_Load(object sender, EventArgs e) {                  
            ServiceClient teenus = new ServiceClient();
            kohvikud = teenus.getEntityKohvikList();
            kuvaKohvikuteAsukohad(kohvikud);
            loaded = true;

            GMap1.addControl(new GControl(GControl.preBuilt.GOverviewMapControl));
            GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl));
            GMap1.setCenter(latlngTln, 12, Subgurim.Controles.GMapType.GTypes.Normal);
       }


        private void kuvaKohvikuteAsukohad(List<EntityKohvik> kohvikud) {
                try {
                    foreach (EntityKohvik k in kohvikud) {                      
                        marker = new Subgurim.Controles.GMarker(new GLatLng(double.Parse(k.Latitude, new System.Globalization.CultureInfo("en-US")), double.Parse(k.Longitude, new System.Globalization.CultureInfo("en-US"))));
                        if (loaded == false) {
                            markerCafeList.Add(new MarkerCafe(marker, k));
                        }
                        GMap1.addGMarker(marker);
                        GInfoWindow window = new GInfoWindow(marker, "<center><b>" + k.Nimi + "</b><br /><a href='kohvik.aspx?kohvikID=" + k.Kohvik_ID + "'>vaata</a></center>", false);
                        GMap1.addInfoWindow(window);
                        
                    }
                }
                catch (Exception) { }      
        }

       
        protected void btn_vaata_Click(object sender, EventArgs e) {
            lbl_error.Text = "";
            GMap1.reset();
            kuvaKohvikuteAsukohad(kohvikud);
            GMap1.addControl(new GControl(GControl.preBuilt.GOverviewMapControl));
            GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl));
            
            String linn = txtLinn.Text;
            String aadress = txtAddress.Text;
            String tanav = txtTanav.Text;
     
            if (aadress.Length != 0) {
                GeoCode koordinaadid = GMap1.getGeoCodeRequest(aadress + "," + tanav + "," + linn +", Eesti");
                LatLngMina = new Subgurim.Controles.GLatLng(koordinaadid.Placemark.coordinates.lat, koordinaadid.Placemark.coordinates.lng);
                if (LatLngMina==latlngTln || LatLngMina==latlngEst || LatLngMina==latlngNull) {
                    GMap1.setCenter(latlngTln, 12, Subgurim.Controles.GMapType.GTypes.Normal);
                    lbl_error.Text = "Ei leitud sellist aadressi";
                }
                else {
                    GIcon icon = new GIcon();
                    icon.image = "images/ikoonGM.png";
                    icon.iconSize = new GSize(20, 28);
                    icon.shadowSize = new GSize(0, 0);
                    icon.iconAnchor = new GPoint(6, 20);

                    GMarkerOptions opt = new GMarkerOptions();
                    opt.draggable = true;
                    opt.title = "Oled siin";
                    opt.icon = icon;

                    markerMina = new Subgurim.Controles.GMarker(LatLngMina,opt);
                    
                    GMap1.addGMarker(markerMina);
                    GInfoWindow windowMina = new GInfoWindow(markerMina, "<center><b>Oled siin!</b></center>", true);
                    GMap1.addInfoWindow(windowMina);
                    Subgurim.Controles.GMarker markerKohvik = leiaLahimKohvik(LatLngMina);
                    if (markerKohvik != null) {
                        GMap1.addGMarker(markerKohvik);
                        GInfoWindow windowKohvik = new GInfoWindow(markerKohvik, cafeInfo, true);
                        GMap1.addInfoWindow(windowKohvik);
                        GMap1.setCenter(LatLngMina, 12, Subgurim.Controles.GMapType.GTypes.Normal);
                    }
                    else { //näitab tln
                        GMap1.setCenter(latlngTln, 12, Subgurim.Controles.GMapType.GTypes.Normal);
                        lbl_error.Text = "urror!";
                    }
                }
            }
            else {
                lbl_error.Text = "Sisesta aadress";
                GMap1.setCenter(latlngTln, 12, Subgurim.Controles.GMapType.GTypes.Normal);
            }   
        }


        private GMarker leiaLahimKohvik(GLatLng LatLngMina) {
            
            List<CoDist> coordsObj = new List<CoDist>();
            Subgurim.Controles.GMarker markerKohvik;
 
            if (markerCafeList.Count > 0) {
                for (int i = 0; i < markerCafeList.Count; i++) {                 
                    coordsObj.Add(new CoDist(markerCafeList[i],LatLngMina.distanceFrom(markerCafeList[i].getMarker().point)));
                }

                sortDistanceAscending sortija = new sortDistanceAscending();
                coordsObj.Sort(sortija);
       
                markerKohvik = coordsObj[0].getMarkerCafe().getMarker();
                cafeInfo = "<center><span style='color:red';>Lähim kohvik!</span><br /><strong>" + coordsObj[0].getMarkerCafe().getKohvik().Nimi + "</strong><br /><a href='kohvik.aspx?kohvikID="+coordsObj[0].getMarkerCafe().getKohvik().Kohvik_ID+"'>Vaata<a/></center>";
             
            }
            else {
                markerKohvik = null;
            }
            
            return markerKohvik;
        }

        public class CoDist {

            MarkerCafe mc;
            Double distance;

            public CoDist(MarkerCafe mc, double distance) {
                this.mc = mc;
                this.distance = distance;
            }

            public MarkerCafe getMarkerCafe() {
                return mc;
            }

            public double getDistance() {
                return distance;
            }         
        }


        public class MarkerCafe {

            GMarker marker;
            EntityKohvik kohvik;

            public MarkerCafe(GMarker marker, EntityKohvik kohvik) {
                this.marker = marker;
                this.kohvik = kohvik;
            }

            public GMarker getMarker() {
                return marker;
            }

            public EntityKohvik getKohvik() {
                return kohvik;
            }
        }


        public class sortDistanceAscending : IComparer<CoDist> {
            public int Compare(CoDist a, CoDist b) {
                CoDist c1 = (CoDist)a;
                CoDist c2 = (CoDist)b;
                if (c1.getDistance() > c2.getDistance())
                    return 1;
                if (c1.getDistance() < c2.getDistance())
                    return -1;
                else
                    return 0;
            }
        }

    }
}
